【例子介绍】
企业内部信息系统如果有升级版本时,需将相应的界面放在服务器端,客户端登录时会提示更新升级,点击升级会将服务器对应的模块拷贝至相应的本机目录。
【相关图片】
【源码结构】
procedure TFrmClientUpdate.updateClick(Sender:TObject);
var
i:integer;
k:word;
begin
try
screen.Cursor := crhourglass;
DataUpdatestr.Clear;
ClientUpdatestr.Clear;
rzmemo1.Clear;
aPath := ExtractFilePath(Application.Exename) 'WorkCenterSystem';
if aPath[Length(aPath)] <> '\' then aPath := aPath '\';
rzmemo1.Lines.Add('正在启动更新会话。');
rzmemo1.Lines.Add('正在检查更新版本信息...');
aCloseTime := 30;
Timer1.Enabled := false;
Label1.Visible := false;
Label3.Visible := false;
Label2.Visible := true;
ProgressBar1.Visible := true;
ProgressBar1.Percent := 8;
rzmemo1.Lines.Add('正在获取数据库上的更新信息。');
try
with query1 do
begin
if active then close;
sql.Text := 'Select FDllName,FVerison,FVerInt From V_AllDllVersion';
open;
first;
while not eof do
begin
if StrtoInt(query1['FVerInt']) > GetFileVersionInt(aPath query1['FDllName']) then
DataUpdatestr.Add(query1['FDllName'])
else if FileExists(aPath query1['FDllName']) = false then
DataUpdatestr.Add(query1['FDllName']);
next;
end;
close;
if DataUpdatestr.Count = 0 then
begin
ProgressBar1.Percent := 100;
Timer1.Enabled := true;
Label2.Visible := false;
Label1.Visible := true;
Label3.Visible := true;
rzmemo1.Lines.Add('更新结束。');
Application.MessageBox(PChar('系统已经是最新版本!'),PChar('提示'),MB_ICONERROR Or MB_OK);
application.Terminate;
end;
end;
except
on E:Exception do
begin
FrmClientUpdate.FormStyle := fsNormal;
Application.MessageBox(pchar('系统出错,请重新更新系统!' #13 '提示:' E.Message),'提示', MB_ICONERROR or MB_OK);
application.Terminate;
exit;
end;
end;
try
if DataUpdatestr.Count = 0 then
application.Terminate;
ProgressBar1.Percent := 10;
rzmemo1.Lines.Add('正在加载最新系统内容........');
rzmemo1.Lines.Add('复制远程文件覆盖到本地:');
try
K := (80 div DataUpdatestr.Count);
except
application.Terminate;
end;
for i := 0 to DataUpdatestr.Count - 1 do
begin
try
IdHTTP1.Head(ClientPath DataUpdatestr.Strings[i]);
except
Application.MessageBox(PChar('系统服务器上不存在此文件!' #13 ClientPath DataUpdatestr.Strings[i]),PChar('提示'),MB_ICONERROR Or MB_OK);
// raise Exception.Create('系统服务器上不存在此文件!' #13 ClientPath DataUpdatestr.Strings[i]);
application.Terminate;
exit;
end;
// if FileExists(aPath DataUpdatestr.Strings[i]) then Deletefile(aPath DataUpdatestr.Strings[i]);
Update(ClientPath DataUpdatestr.Strings[i],aPath DataUpdatestr.Strings[i]);
ProgressBar1.Percent := ProgressBar1.Percent k;
rzmemo1.Lines.Add(aPath DataUpdatestr.Strings[i]);
end;
ProgressBar1.Percent := 94;
// if FileExists(fpath 'ClientSystemInfo.ini') then Deletefile(fpath 'ClientSystemInfo.ini');
// Update(ClientPath 'ClientSystemInfo.ini',fpath 'ClientSystemInfo.ini');
ProgressBar1.Percent := 100;
//#ToDo1 :创建更新日志文件夹并写入更新日志 --tendy_tang
if not DirectoryExists(ExtractFilePath(paramstr(0)) 'UpdateLog') then
CreateDir(PChar(ExtractFilePath(paramstr(0)) 'UpdateLog'));
fpath := ExtractFilePath(paramstr(0)) 'UpdateLog';
if fpath[Length(fpath)] <> '\' then fpath := fpath '\';
sClientUpdateName := 'ClientUpdateInfo' FormatDateTime('yyyymmddhhmm',Now) '.ini';
ini := TiniFile.Create(fpath sClientUpdateName);
ini.WriteString('UpdateInfos', 'UpdateLog', sUpdateInfo);
// sUpdateLog := ini.ReadString(sClientUpdateName,'UpdateLog',sUpdateInfo);
// showmessage(sUpdateLog);
rzmemo1.Lines.Add('总共更新文件数: ' inttostr(DataUpdatestr.Count) ' 个');
rzmemo1.Lines.Add('系统更新成功!');
Timer1.Enabled := true;
Label2.Visible := false;
Label1.Visible := true;
Label3.Visible := true;
btnclose.SetFocus;
except
on E:Exception do
begin
rzmemo1.Lines.Add('更新失败:' aPath DataUpdatestr.Strings[i]);
rzmemo1.Lines.Add('出错原因:文件正在使用');
MessageBox(0, pchar('提示:' E.Message),
'提示', MB_ICONERROR or MB_OK);
application.Terminate;
exit;
end;
end;
finally
screen.Cursor := crdefault;
end;
end;
评论